package com.epam.tcs.actions.pages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.tcs.actions.BreadCrumb;
import com.epam.tcs.dao.factory.DAO;
import com.epam.tcs.dao.hibernate.entities.Theme;

public class ThemePage extends PageAbstract {

	@Override
	public void doExec(HttpServletRequest req, HttpServletResponse res) {

		String target = req.getRequestURI().substring(
				req.getContextPath().length());

		String[] tarr = target.split("/");

		BreadCrumb.add(req, res, "Темы", "/tcs/theme");
		try {
			if ((tarr.length == 4) && tarr[3].equals("add")) {
				addTheme(req, res);
			} else if ((tarr.length == 4) && tarr[3].equals("del")) {
				delTheme(req, res);
			} else if ((tarr.length == 4) && tarr[3].equals("edit")) {
				editTheme(req, res);
			} else {
				listThemes(req, res);
			}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void listThemes(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		DAO<Theme> theme = dao.getThemeDAO();
		req.setAttribute("widget", "theme");
		req.setAttribute("action", "");
		req.getSession().setAttribute("listThemes", theme.getAllElements());
		outputPage("theme/theme.jsp", req, res);
	}

	private void editTheme(HttpServletRequest req, HttpServletResponse res) {
		// TODO Auto-generated method stub

	}

	private void delTheme(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		DAO<Theme> theme = dao.getThemeDAO();
		ArrayList<String> themes_name = new ArrayList<String>();
		for (Enumeration<String> e = req.getParameterNames(); e
				.hasMoreElements();) {
			String[] s = ((String) e.nextElement()).split("_");
			Theme theme_element = (Theme) theme
					.getElement(Long.parseLong(s[2]));
			themes_name.add(theme_element.getName());
			theme.deleteElement(theme.getElement(Long.parseLong(s[2])));
		}
		req.setAttribute("widget", "theme");
		req.setAttribute("message", "Удалены темы: " + themes_name.toString());
		req.getSession().setAttribute("listThemes", theme.getAllElements());
		outputPage("theme/theme.jsp", req, res);
	}

	private void addTheme(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		BreadCrumb.add(req, res, "Добавить тему", "/tcs/theme/add");
		if (req.getMethod() == "POST" && this.validateThemeForm(req)) {
			DAO<Theme> theme = dao.getThemeDAO();
			Theme t = new Theme();
			t.setName(req.getParameter("caption"));
			t.setHours(Integer.parseInt(req.getParameter("hours")));
			t.setDescription(req.getParameter("description"));

			if (theme.addElement(t) != 0L) {
				req.setAttribute("message", "Тема \"" + t.getName()
						+ "\" успешно добавлена.");
			}
		}
		req.setAttribute("widget", "add_theme");
		req.setAttribute("action", "/tcs/theme/add");
		outputPage("theme/theme_form.jsp", req, res);

	}

	private boolean validateThemeForm(HttpServletRequest req) {
		// TODO Auto-generated method stub
		return true;
	}

}
